package com.jdbc.web;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.jdbc.entity.Product;
import com.jdbc.util.DBUtil;

public class ConnectionTest2 {

	public static void main(String[] args) {
		Product p=new Product();
		p.setId(10);
		p.setName("叶子叶子");
		p.setPrice(29);
		//saveProduct(p);hhhh
		//deleteProductById(5);
		updateProduct(p);

	}

	public static void updateProduct(Product p) {
		Connection conn=DBUtil.getConnection();
		PreparedStatement ps=null;
		try {
			 ps=conn.prepareStatement("update  product set  name=?,price=? where id=?");
			
			 ps.setString(1, p.getName());
			 ps.setInt(2, p.getPrice());
			 ps.setInt(3, p.getId());
			 int rs=ps.executeUpdate();
			 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

	public static void deleteProductById(int id) {
		Connection conn=DBUtil.getConnection();
		PreparedStatement ps=null;
		try {
			 ps=conn.prepareStatement("delete from product where id=?");
			 
			 ps.setInt(1, id);
			 
			 int rs=ps.executeUpdate();
			 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}

	public static void saveProduct(Product p) {
		Connection conn=DBUtil.getConnection();
		PreparedStatement ps=null;
		try {
			 ps=conn.prepareStatement("insert into product(id,name,price) values(?,?,?)");
			 ps.setInt(1, p.getId());
			 ps.setString(2, p.getName());
			 ps.setInt(3, p.getPrice());
			 int rs=ps.executeUpdate();
			 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}

}
